﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ContourBuilder.Components;
using System.Windows.Forms;
using ITAP.ContourBuilder.Components.Dimensions;
using ITAP.Core.Modelling;


namespace ITAP.ContourBuilder.Manipulation
{
    public class CommandAddDirectDistanceDimension : SerializedCommand
    {
        private List<Type> selectPriority;
        IEnumerable<IContourComponent> allComponents;

        public CommandAddDirectDistanceDimension(ContourControl control)
            : base(control)
        {
            selectPriority = new List<Type>();
            selectPriority.Add(typeof(ContourConnector));
            selectPriority.Add(typeof(ContourHole));

            allComponents = this.control.ContourContainer.GetAllInnerComponents();
        }

        IContourComponent component1 = null;
        IContourComponent component2 = null;
        private DirectDistanceDimension dimension = null;
        public override void ObtainPeripheryState(GlobalPeripheryState state)
        {
            base.ObtainPeripheryState(state);

            if (state.KeyboardState.KeyMode == KeyMode.Down && state.KeyboardState.KeyState.KeyCode == Keys.Escape)
            {
                this.Cancel();
                return;
            }

            if (state.MouseState.MouseMode == MouseMode.Down && state.MouseState.State.Button == MouseButtons.Left)
            {
                ModelPoint point = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                foreach (Type type in selectPriority)
                {
                    foreach (IContourComponent component in allComponents)
                    {
                        if (component.GetType() != type)
                            continue;

                        if (component1 == null)
                            if (component.CanSelectByPoint(point))
                            {
                                component1 = component;
                                return;
                            }

                        if (component2 == null)
                            if (component != component1 && component.CanSelectByPoint(point))
                            {
                                component2 = component;

                                dimension = (DirectDistanceDimension)DimensionFactory.CreateDirectDistanceDimension(component1, component2, GlobalData.DimensionTextFont, "");
                                component1.GetRootModel().AddInnerComponent(dimension);
                                this.control.UpdateModel();
                                this.Exit();
                                return;
                            }
                    }
                }
            }
            else if (state.MouseState.MouseMode == MouseMode.Up && state.MouseState.State.Button == MouseButtons.Left)
            {
                if (component1 != null && component2 != null)
                    this.Exit();
            }
        }

        public override string ToString()
        {
            return "Создать размер дистанции";
        }
    }
}
